package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.nio.channels.Channel;
import java.util.Date;

@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
	@Autowired
	private WmChannelMapper wmChannelMapper;
	@Autowired
	private WmNewsMapper wmNewsMapper;
	
	@Override
	public ResponseResult getChannels() {
		Page<WmChannel> page = new Page<>(1, 100);
		Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, null);
		return ResponseResult.okResult(wmChannelPage.getRecords());
	}
	
	@Override
	public ResponseResult channel(WmChannel wmChannel) {
		if (wmChannel == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
		}
		String name = wmChannel.getName();
		LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(!StringUtils.isEmpty(name), WmChannel::getName, name);
		Integer count = wmChannelMapper.selectCount(wrapper);
		if (count >= 1) {
			//大于等于1 ,表示当前名频道已存在,直接返回频道已存在
			return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道名已存在");
		} else {
			//新增频道
			wmChannel.setCreatedTime(new Date());
			// wmChannel.set
			int insert = wmChannelMapper.insert(wmChannel);
			if (insert >= 1) {
				return ResponseResult.okResult("新增频道成功");
			} else {
				return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "新增频道失败");
			}
		}
		
		
	}
	
	@Override
	public ResponseResult channelList(ChannelDto dto) {
		if (dto == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数为空");
		}
		String name = dto.getName();
		Integer page = dto.getPage();
		Integer size = dto.getSize();
		page = (page == 0) ? 1 : page;
		size = (size == 0) ? 1 : size;
		
		Page<WmChannel> wrapperPage = new Page<>(page, size);
		LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
		wrapper.like(!StringUtils.isEmpty(name), WmChannel::getName, name);
		
		Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(wrapperPage, wrapper);
		
		ResponseResult<Object> objectResponseResult = new ResponseResult<>();
		objectResponseResult.setData(wmChannelPage.getRecords());
		
		return objectResponseResult;
	}
	
	//更新channel
	@Override
	public ResponseResult channelUpdate(WmChannel wmChannel) {
		if (wmChannel == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
		}
		LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
		
		wrapper.eq(wmChannel.getId() != null, WmNews::getChannelId, wmChannel.getId());
		Integer newsNum = wmNewsMapper.selectCount(wrapper);
		if (newsNum >= 1) {
			//如果被引用则不能被修改
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已被引用,不可修改");
		} else {
			//没有被引用,可以修改
			wmChannelMapper.updateById(wmChannel);
			return ResponseResult.okResult("修改成功");
		}
		//
	}
	
	@Override
	public ResponseResult channelDelete(Long id) {
		//参数校验
		if (id == null) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道ID不存在");
		}
		//判断是否被引用
		LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(WmNews::getChannelId, id);
		Integer count = wmNewsMapper.selectCount(wrapper);
		if (count >= 1) {
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已被引用,无法删除");
		}
		//判断状态是否被引用
		LambdaQueryWrapper<WmChannel> wrapper1 = new LambdaQueryWrapper<>();
		wrapper1.eq(WmChannel::getId, id);
		WmChannel wmChannel = wmChannelMapper.selectOne(wrapper1);
		if (wmChannel.getStatus() == true) {
			//未被禁用
			return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道已被启用");
		} else {
			//已被禁用
			wmChannelMapper.deleteById(id);
			return ResponseResult.okResult("操作成功");
		}
		
		
	}
}